package com.example.myapplication.util

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.content.ContextCompat
import com.example.myapplication.ui.PermissionRequestScreen

@SuppressLint("ContextCastToActivity")
@Composable
fun PermissionCheckWrapper(content: @Composable () -> Unit) {
    val activity = (androidx.compose.ui.platform.LocalContext.current as ComponentActivity)
    var hasPermissions by remember { mutableStateOf(checkPermissions(activity)) }
    var showPermissionRationale by remember { mutableStateOf(false) }

    val permissionLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.RequestMultiplePermissions(),
        onResult = { permissionsMap ->
            val allGranted = permissionsMap.values.all { it }
            hasPermissions = allGranted
            if (!allGranted) {
                Toast.makeText(activity, "缺少必要权限，应用可能无法正常工作", Toast.LENGTH_LONG).show()
            }
        }
    )

    LaunchedEffect(hasPermissions) {
        if (!hasPermissions) {
            showPermissionRationale = shouldShowPermissionRationale(activity)
        }
    }

    if (hasPermissions) {
        content()
    } else {
        PermissionRequestScreen(
            showRationale = showPermissionRationale,
            onRequestPermissions = {
                permissionLauncher.launch(
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                        arrayOf(
                            Manifest.permission.INTERNET,
                            Manifest.permission.READ_MEDIA_IMAGES
                        )
                    } else {
                        arrayOf(
                            Manifest.permission.INTERNET,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                        )
                    }
                )
            }
        )
    }

}
fun checkPermissions(context: ComponentActivity): Boolean {
    val networkPermission = ContextCompat.checkSelfPermission(
        context,
        Manifest.permission.INTERNET
    ) == PackageManager.PERMISSION_GRANTED

    val storagePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        ContextCompat.checkSelfPermission(
            context,
            Manifest.permission.READ_MEDIA_IMAGES
        ) == PackageManager.PERMISSION_GRANTED
    } else {
        ContextCompat.checkSelfPermission(
            context,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) == PackageManager.PERMISSION_GRANTED
    }

    return networkPermission && storagePermission
}

fun shouldShowPermissionRationale(activity: ComponentActivity): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        activity.shouldShowRequestPermissionRationale(Manifest.permission.READ_MEDIA_IMAGES)
    } else {
        activity.shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }
}